#pragma once
#include <iostream>
#include <string>
#include "Protocol.hpp"
enum
{
    DIV_ERR = 1,
    MOD_ERR = 2,
    UNKNOWN = 3
};
class ServerCal
{
public:
    ServerCal()
    {}
    ~ServerCal()
    {}
public:
    Response CalculatorHelper(Request& req)
    {
        Response resp(0, 0);
        switch(req.oper)
        {
            case '+':
                resp.result = req.x + req.y;
                break;
            case '-':
                resp.result = req.x - req.y;
                break;
            case '*':
                resp.result = req.x * req.y;
                break;
            case '/':
                {
                    if(req.x == 0)  resp.exitcode = DIV_ERR;
                    else  resp.result = req.x / req.y;
                }
                break;
            case '%':
                {
                    if(req.x == 0)  resp.exitcode = MOD_ERR;
                    else  resp.result = req.x % req.y;
                }
                break;
            default:
                resp.exitcode = UNKNOWN;
                break;
        }
        return resp;
    }
    std::string Calculator(std::string& package)
    {
        Request req;
        std::string content;
        bool r = Decode(package, &content);
        if(!r)  return "";
        r = req.Deserialize(content);
        if(!r)  return "";
        Response resp = CalculatorHelper(req);

        //序列化
        content = "";
        resp.Serialize(&content);
        content = Encode(content);
        return content;
    }

};